<?php
namespace Admin\Controller;
use Think\Controller;
class CarController extends Controller {
    public function CarPage(){
        $CarFactory =M('Car');
        $Car =  $CarFactory->select();
        $this->assign('Car',$Car);
       $this->display('CarPage');
    }
    public function CarSearch_action(){
        $CarFactory = M('Car');
        /*通过车辆编号的查询*/
        if($_POST['reg']=='车辆编号'){
            $Car_Id = $_POST['searchCon'];
            $Car = $CarFactory->where("Car_Id='$Car_Id'")->find();
            if($Car == null)
                $Car['success'] = 0;
            else {
                $Car['success'] = 1;
                echo json_encode($Car);
            }
        }
    }
    /*通过品牌查询车辆信息*/
    public function brand_search(){
        $CarFactory =M('Car');
        $Car_Brand = $_GET['Car_Brand'];
        $Car =  $CarFactory->where("Car_Brand='$Car_Brand'")->select();
        $this->assign('Car',$Car);
        $this->display('CarPage');
    }
    /*通过品牌查询车辆信息*/
    public function type_search(){
        $CarFactory =M('Car');
        $Car_Type = $_GET['Car_Type'];
        $Car =  $CarFactory->where("Car_Type='$Car_Type'")->select();
        $this->assign('Car',$Car);
        $this->display('CarPage');
    }
    /*显示车辆操作界面*/
    public function CarManage(){
        if($_GET['Car_Id']==null){
            $this->error('管理操作车辆界面失败，请先选择您要操作的车辆','/Car/CarPage');
        }
        else {
            $CarFactory = M('Car');
            $Car_Id = $_GET['Car_Id'];
            $Car = $CarFactory->where("Car_Id='$Car_Id'")->find();
            $this->assign('Car', $Car);
            $this->display('CarManage');
        }
    }
    /*上架操作控制*/
    public function CarUp(){
        $data = [];
        $CarFactory = M('Car');
        $Car_Id = $_GET['Car_Id'];
        $data['Car_Status']='上架中';
       $CarFactory->where("Car_Id='$Car_Id'")->save($data);
        //die(U('Car/CarManage',['Car_Id'=>$Car_Id]));
       $this->success("上架成功，将返回操作界面",'/Car/CarManage?Car_Id='.$Car_Id);
    }

    /*下架操作控制*/
    public function CarDown(){

        $data = [];
        $CarFactory = M('Car');
        $Car_Id = $_GET['Car_Id'];
        $data['Car_Status']='已下架';
        $CarFactory->where("Car_Id=$Car_Id")->save($data);
        //die(U('Car/CarManage',['Car_Id'=>$Car_Id]));
        $this->success("下架成功，将返回操作界面",'/Car/CarManage?Car_Id='.$Car_Id);
    }

     /*显示增加车辆的页面*/
    public function  CarAdd(){
        $this->display('CarAdd');
    }

    /*增加车辆的处理页面*/
    public  function  CarAdd_action(){
         $CarFactory = M('Car');
         $newfile ="/Public/CarImages/".$_FILES['img']['name'];
         $oldfile = $_FILES['img']['tmp_name'];
         move_uploaded_file($oldfile,$newfile);
    }
}